<template>
    <el-container>
        <!-- 头部区域 -->
        <el-header>
          <div>
            <img src="../assets/logo.png" alt="">
            <span>电商管理系统</span>
          </div>
          <el-button type="info" @click="loginOut">退出</el-button>
        </el-header>
        <!-- 页面主体内容区域 -->
        <el-container>
          <!-- 侧边栏 -->
          <el-aside :width="isCollaps ? '64px' : '200px'">
              <!-- 侧边栏菜单区域 -->
              <div class="toggle-button" @click="toggleCollapse">
                <span>|||</span>
              </div>
              <el-menu
              background-color="#606266"
              text-color="#ffffff"
              unique-opened
              :collapse="isCollaps"
              :collapse-transition="false"
              router
              :default-active="activePath"
              >
                <!-- 一级菜单区域 -->
                <el-submenu
                v-for="(item1, index) in menuList"
                :key="item1.id"
                :index="item1.id+''"
                >
                  <!-- 一级菜单的模版区域 -->
                  <template slot="title">
                    <!-- 图标 -->
                    <i :class="iconObj[index]"></i>
                    <!-- 文本 -->
                    <span>{{item1.authName}}</span>
                  </template>
                  <!-- 二级菜单 -->
                  <el-menu-item
                  v-for="item2 in item1.children"
                  :key="item2.id"
                  :index="'/' + item2.path"
                  @click="saveNavState('/' + item2.path)"
                  >
                  <!-- 二级菜单的模版区域 -->
                    <template slot="title">
                      <i class="el-icon-menu"></i>
                      <span>{{item2.authName}}</span>
                    </template>
                  </el-menu-item>
                </el-submenu>
              </el-menu>
          </el-aside>
          <!-- 右侧内容主体 -->
          <el-main>
            <!-- 路由占位符 -->
            <router-view></router-view>
          </el-main>
        </el-container>
    </el-container>
</template>

<script>

export default {
  name: 'Home',
  props: {

  },
  data () {
    return {
      // 左侧菜单数据
      menuList: [],
      iconObj: [
        'iconfont icon-user',
        'iconfont icon-tijikongjian',
        'iconfont icon-shangpin',
        'iconfont icon-danju-tianchong',
        'iconfont icon-baobiao'
      ],
      // 是否折叠
      isCollaps: false,
      // 被激活的地址
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    // 检测当前被激活的地址
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginOut () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有菜单数据
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    toggleCollapse () {
      this.isCollaps = !this.isCollaps
    },
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style scoped>
.el-container{
  height: 100%;
}
.el-header{
  background-color: #303133;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  color: #FFFFFF;
  font-size: 20px;
}
.el-header div{
  display: flex;
  align-items: center;
  height: 100%;
}
.el-header div img{
  height: 80%;
}
.el-header div span{
  padding-left: 20px;
}
.el-aside{
  background-color: #606266;
}
.el-aside .el-menu{
  border-right: none;
}
.el-main{
  background-color: #DCDFE6;
  padding: 20px;
}
.iconfont{
  margin-right: 10px
}
.toggle-button{
  background-color: #4a5064;
  font-size: 10px;
  height: 30px;
  line-height: 30px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
